package com.resrun.controller;

import com.resrun.service.image.EntSealClipService;
import com.resrun.service.image.EntSealGenerateService;
import com.resrun.service.pdf.SignService;
import com.resrun.service.verify.SignVerifyService;
import com.resrun.utils.Base64;
import com.resrun.vo.base.Result;
import com.resrun.vo.request.*;
import com.resrun.vo.response.SealResponse;
import com.resrun.vo.response.SignResponse;
import com.resrun.vo.response.VerifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * PDF签署
 *
 * @author hupan
 * @since 2024-01-25
 */
@Api(tags = "开放签-演示demo")
@Slf4j
@Validated
@RestController
@RequestMapping("/sign")
@AllArgsConstructor
public class SignController {

    private SignService signService;
    private SignVerifyService signVerifyService;
    private EntSealGenerateService entSealGenerateService;
    private EntSealClipService entSealClipService;


    @ApiOperation("生成企业签章-上传生成")
    @PostMapping(value = "/clip/seal")
    public Result<SealResponse> generateUpload(@RequestBody ClipSealRequest request) {
        if (request.getImage() == null || request.getImage().isEmpty()) {
            return Result.error("图片数据为空", null);
        }
        byte[] decode = Base64.decode(request.getImage());
        if (ArrayUtils.isEmpty(decode)) {
            return Result.error("签章制作失败", null);
        }

        byte[] entSealByte = entSealClipService.clip(decode, request.getColorRange());
        if (entSealByte == null) {
            return Result.error("签章制作失败", null);
        }
        String encode = Base64.encode(entSealByte);
        SealResponse response = new SealResponse();
        response.setEntSeal(encode);
        return Result.ok(response);
    }

    @ApiOperation("生成企业签章-参数生成")
    @PostMapping(value = "/generate/seal")
    public Result<SealResponse> seal(@RequestBody GenerateSealRequest request) {
        if (request == null || request.getMiddleText() == null || request.getTopText() == null) {
            return Result.error("参数缺失", null);
        }
        byte[] entSealByte = entSealGenerateService.generateEntSeal(request.getTopText(), request.getMiddleText());
        if (entSealByte == null) {
            return Result.error("签章制作失败", null);
        }
        String encode = Base64.encode(entSealByte);
        SealResponse response = new SealResponse();
        response.setEntSeal(encode);
        return Result.ok(response);
    }

    @ApiOperation("PDF签署（个人签名）")
    @PostMapping(value = "/signature")
    public Result<SignResponse> signWithSignature(@Valid @RequestBody SignatureSignRequest request) {
        try {
            return Result.ok(signService.signPersonal(request));
        } catch (Exception e) {
            log.error("签署异常", e);
            return Result.error("签署异常");
        }
    }

    @ApiOperation("PDF签署（企业印章）")
    @PostMapping(value = "/seal")
    public Result<SignResponse> signWithSeal(@Valid @RequestBody SealSignRequest request) {
        try {
            return Result.ok(signService.signEnterprise(request));
        } catch (Exception e) {
            log.error("签署异常", e);
            return Result.error("签署异常");
        }
    }

    @ApiOperation("文件验签")
    @PostMapping(value = "/file/verify")
    public Result<VerifyResponse> fileVerify(@RequestParam(value = "file") MultipartFile file) {
        if (file == null || StringUtils.isBlank(file.getName())) {
            return Result.error("文件不能为空");
        }

        try {
            VerifyResponse imageFromPdf = signVerifyService.getImageFromPdf(file.getBytes(), file.getName());
            return Result.ok(imageFromPdf);
        } catch (IOException e) {
            return Result.error("文件处理异常");
        }
    }

    @ApiOperation("文件验签")
    @PostMapping(value = "/verify")
    public Result<VerifyResponse> verify(@RequestBody VerifyRequest request) {
        String verifyFile = request.getVerifyFile();
        byte[] decode = Base64.decode(verifyFile);
        VerifyResponse imageFromPdf = signVerifyService.getImageFromPdf(decode, request.getFileName());

        return Result.ok(imageFromPdf);
    }

}